require(['select2', 'date', 'validator', 'grid'], function () {
    var cnt = 0,organId,
        $drvExamForm = $("#drvExamForm"),
        $drvExamTab = $("#drvExamTab");


    add = function (fn) {
        addOrUpdate("/baseDriverExamInfo/addDrvExam.htm", fn);
    };

    reset = function (lineIds) {
        $drvExamForm.reset($drvExamForm);
        // 新增驾驶员考勤记录
        var lineId = lineIds[0];
        // 准备数据
        $.ajaxExt({
            url: "/baseDriverExamInfo/getLineInfoByLineId.htm",
            data: {'lineId': lineId}
        }).done(function (data) {
            var lineEntity = data;
            var lineName = lineEntity.lineName;
            $("#lineNameSpan").text(lineName);
            $("#lineId").val(lineId);
            organId = lineEntity.organId;
            if (!cnt) {
                $('#addRowA').click();
            }
        });
    };


    edit = function (fn) {
        addOrUpdate("/baseDriverExamInfo/uptDrvExam.htm", fn);
    };

    setVal = function (rowData) {
        $("#uptAttendId").val(rowData.attendId);
        $("#uptLineName").text(rowData.lineName);
        $("#uptLicPltNo").text(rowData.carName);
        $("#uptDrvName").text(rowData.drvName);
        $("#uptSignInTime").val(rowData.signInTime);
        $("#uptSignOutTime").val(rowData.signOutTime);
    };

    function addOrUpdate(url, callBack) {
        var result = $.validator($drvExamForm);
//        if (result) {
//            $.messager.confirm("是否保存", "是否新增驾驶员考勤信息？", function (r) {
//                if (r) {
                    $.ajaxExt({
                        url: url,
                        data: $drvExamForm.serialize()
                    }).done(function (data) {
//                    	console.info(data);
                    	$.messager.alert("提示",data, "info",function(){
                			this.__closeSelf();
                		});
                    });
//                }
//            });
//        }
    }

    $drvExamTab.on("click.addRowA", "#addRowA", function () { // 添加行的点击事件
        cnt++;
        //var newTR = "<tr id='newTR'>"
        //    + "<td style='border-left: 1px solid #DCDCDC' class='ac'><select style='width: 100px;' id='carIds' name='carIds' data-options='{url:'/baseCarInfo/getBaseCarInfoList.htm',textField:'licPltNo',idField:'carId',param:'organId'}' data-validator='{required:true}'></select></td>"
        //    + "<td class='ac'><select style='width: 90px;' id='drvIds' name='drvIds' data-options='{url:'/baseDrvInfo/getBaseDrvInfoList.htm',textField:'drvName',idField:'drvId',param:'organId'}' data-validator='{required:true}'></select></td>"
        //    + "<td class='ac'><div class='select-box select-box-large'><input type='text' id='add_beginTime0" + cnt + "' name='signInTimes' data-validator='{required:true}' class='u-input' style='width:130px' onclick=\"WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})\"><span class='icon-date'></span></div></td>"
        //    + "<td class='ac'><div class='select-box select-box-large'><input type='text' id='add_endTime0" + cnt + "' name='signOutTimes' data-validator=\"{dateCompare:'add_beginTime0" + cnt + "',required:true}\" class='u-input' style='width:130px' onclick=\"WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})\"><span class='icon-date'></span></div></td>"
        //    + "<td class='ac'><a class='operate operate-delete delRowA' href='javascript:;'>删除</a></td>"
        //    + "</tr>";
        var newTR =
            ('<tr>' +
            '   <td style="border-left: 1px solid #DCDCDC" class="ac">' +
            '       <select style="width: 100px;" name="carIds" data-options="{url:\'/baseCarInfo/getBaseCarInfoList.htm?organId={0}\',textField:\'licPltNo\',idField:\'carId\'}" data-validator="{required:true}"></select>' +
            '   </td>' +
            '   <td class="ac">' +
            '       <select style="width: 90px;"  name="drvIds" data-options="{url:\'/baseDrvInfo/getBaseDrvInfoList.htm?organId={0}\',textField:\'drvName\',idField:\'drvId\'}" data-validator="{required:true}"></select>' +
            '   </td>' +
            '   <td class="ac">' +
            '       <div class="select-box select-box-large">' +
            '           <input type="text" id="add_beginTime{1}" name="signInTimes" data-validator="{required:true}" class="u-input" style="width:130px" onclick="WdatePicker({isShowClear:true,dateFmt:\'yyyy-MM-dd HH:mm:ss\'})"><span class="icon-date"></span>' +
            '        </div>' +
            '   </td>' +
            '   <td class="ac">' +
            '       <div class="select-box select-box-large">' +
            '           <input type="text" name="signOutTimes" data-validator="{dateCompare:\'add_beginTime{1}\',required:true}" class="u-input" style="width:130px" onclick="WdatePicker({isShowClear:true,dateFmt:\'yyyy-MM-dd HH:mm:ss\'})"><span class="icon-date"></span>' +
            '       </div>' +
            '   </td>' +
            '   <td class="ac"><a class="operate operate-delete delRowA" id="delRowA">删除</a></td>' +
            '</tr>').format(organId, cnt);
        $(newTR).insertBefore($('#endTR')).initForm();
        var $newTR = $("#newTR");
        $newTR.attr({id: "newTR"});
    }).on("click.delRowA", ".delRowA", function () { // 删除行的点击事件
        if ($("#drvExamTab tr").length == 3) {
            $.messager.alert("提示", "这是最后一条信息了，请不要删除。", "info");
        } else {
            var delTR = $(this).closest('tr');
            if (delTR.attr("id") === "newTR") {
                delTR.prev().attr({id: "newTR"});
            }
            delTR.remove();
        }
    });

    $drvExamForm.initForm();
});